#include "client.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <sys/types.h>    
#include <sys/socket.h>
int main(int argc, const char *argv[])
{  	
	ER_TYPE ret = 0;
	
	
	ret = net_init();
	if(ERROR == ret)
	{
	printf("net_init fail\n");
	exit(EXIT_FAILURE);
	}
	

	init_display();


	return 0;
}

/**
 *网络初始化函数
 *功能：与服务端建立链接
 *
 *返回：失败：ERROR
 *      成功：OK
 */
ER_TYPE net_init()
{
	
	socketid = socket(AF_INET,SOCK_STREAM,0);
	if(socketid<0){
		fprintf(stderr,"socket fail ;%s\n",strerror(errno));
		return ERROR;
	}

	struct sockaddr_in  socketcli;
	socketcli.sin_addr.s_addr = inet_addr("192.168.0.108");
	socketcli.sin_port = htons(11107);
	socketcli.sin_family = AF_INET;

	
	if(connect(socketid,(struct sockaddr *)&socketcli,sizeof(struct sockaddr))<0){	
	fprintf(stderr,"connect fail:%s\n",strerror(errno));
		return ERROR;
	}
	return OK;
}

/**
 *数据接收函数
 *
 *功能：接收服务端数据 接收数据由buf返回
 *返回：失败：ERROR
 *      成功：OK
 *
 */

ER_TYPE do_recv(struct msg *buf)
{

	if(recv(socketid,buf,sizeof(struct msg),0)<0)
	{
	printf("recv fail\n");
	return ERROR;
	}
	return OK;
}

/**
 *数据发送函数
 *
 *功能：发送数据至服务端 数据：buf
 *返回：失败：ERROR
 *      成功：OK
 *
 */
ER_TYPE do_send(struct msg *buf)
{
	if(send(socketid,buf,sizeof(struct msg),0)<0)
	{
	printf("send fail\n");
	return ERROR;
	}
	printf("name:%s\n",buf->user_info.name);
	printf("pass:%s\n",buf->user_info.password);
	return OK;
}
